import { ref, shallowRef } from 'vue';
import { definePlugin } from '@/components/@comp/create-component';

export default definePlugin({
  name: 'request',
  priority: 'TOOL',
  extendProps: {
    table: Object,
    request: Object,
  },
  useMain(_instance, props) {
    // console.log(props);
    const { table: tableProps = {} } = props;

    const data = shallowRef({
      list: tableProps.data || [],
      total: (tableProps.data || []).length,
    });

    const page = ref({
      pageNumber: tableProps.pagination?.current || 1,
      pageSize: tableProps.pagination?.pageSize || 10,
    });

    const search = (_params) => {
      setPage((page) => ({ ...page, pageNumber: 1 }));
    };

    const goto = (pageNumber, pageSize) => {
      setPage({ pageNumber, pageSize });
    };

    return {
      request: {
        loading: tableProps.loading || false,
        data: data,
        page,
        search,
        goto,
      },
    };
  },
});
